package com.buyingtickets.service.impl;

import com.buyingtickets.dao.AdminSiteDao;
import com.buyingtickets.dao.SiteDao;
import com.buyingtickets.entity.QueryParameter;
import com.buyingtickets.entity.Site;
import com.buyingtickets.service.SiteService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Hexz
 * @date 2021/6/29 9:39
 */
@Service
public class SiteServiceImpl implements SiteService {
    @Resource
    private SiteDao siteDao;
    @Resource
    private AdminSiteDao adminSiteDao;

    @Override
    public int queryByName(String siteName) {
        return siteDao.queryByName(siteName);
    }






//后台站点分页 增删改查
    @Override
    public List<Site> queryByQuerySite(QueryParameter qp) {
        int size=qp.getRows();//每页显示几条
        int count=queryCountQuerySite(qp);//带条件的总条数
        int countPage=count%size==0?(count/size):(count/size+1);//带条件总页数
        if(countPage==0){
            return null;
        }
        int page=qp.getPage();//当前页
        //上下容错
        page=page<1?1:page;
        page=page>countPage?countPage:page;
        qp.setPage(page);//当前页
        qp.setStart((page-1)*size);//偏移量
        qp.setPageCount(countPage);//总页数
        qp.setShangPage(page-1);
        qp.setXiaPage(page+1);
        qp.setCount(count);
        return adminSiteDao.queryByQuerySite(qp);
    }

    @Override
    public int queryCountQuerySite(QueryParameter qp) {
        return adminSiteDao.queryCountQuerySite(qp);
    }

    @Override
    public Site queryByID(int id) {
        return adminSiteDao.queryByID(id);
    }

    @Override
    public int update(Site site) {
        return adminSiteDao.update(site);
    }

    @Override
    public int delete(int id) {
        return adminSiteDao.delete(id);
    }

    @Override
    public int add(Site site) {
        return adminSiteDao.add(site);

    }

    @Override
    public List<Site> querySiteAll() {
        return adminSiteDao.querySiteAll();
    }

    @Override
    public boolean addSite(String province, String city, String site) {
       if( StringUtils.hasText(province)&&StringUtils.hasText(city)
               &&StringUtils.hasText(site)){
           return siteDao.addSite(province,city,site);
       }
        return false;
    }

    @Override
    public Site queryByCityAndsite(String city, String site) {
        if( StringUtils.hasText(city)&&StringUtils.hasText(site)){
            return siteDao.queryByCityAndsite(city,site);
        }
        return null;
    }

    @Override
    public List<Site> queryAll() {
        return siteDao.queryAll();
    }

    @Override
    public List<Site> queryByProvince(String province) {
        if( StringUtils.hasText(province)){
            return siteDao.queryByCity(province);
        }
        return null;
    }
}
